<?php

namespace App\Service\playlet;
use App\Model\PlayletEpisodes;
use App\Service\CommicService;
use App\Model\Playlet;
use Hyperf\Utils\ApplicationContext;
use App\Common\Response\SystemCode;
class PlayletEpisodesService extends CommicService
{
    const cacheDuration = 60 * 15;
    public function __construct()
    {
        $container = ApplicationContext::getContainer();
        $this->redisService = $container->get(\Hyperf\Redis\Redis::class);
    }
    const STATUS_ENABLE  = 1;
    public function Search(Array $params){

        $keyword     = $params['keyword'] ?? '';
        $playlet_number     = $params['name'] ?? '';
        $pageSize    = $params['pageSize'] ?? 10;
        $page        = $params['page'] ?? 1;
        $playletIds = '';
        if ($keyword) {
            //查询小程序
            $playletIds = Playlet::query()->where([['name', 'like', '%' . $keyword . '%']])->pluck('id');
        }

        $query = PlayletEpisodes::query()
            ->select(['id','playlet_id','playlet_number','cover','play_url','created_at','updated_at']);

        if ($playletIds) {
            $query->whereIn('playlet_id' ,$playletIds);
        }
        if ($playlet_number) {
            //查询集数
            $query->where(['playlet_number' => $playlet_number]);
        }

        $models = $query->orderBy('id','desc')->paginate($pageSize,['*'],'page',$page);
        $items = $models->items();
        $selectData = [];
        foreach ($items as $item){
            $data = $item->toArray();
            if(!empty($item->playlet)){
                $data['playlet_name']  = $item->playlet->name;
            }else{
                $data['playlet_name']  = '';
            }
            $selectData[] =$data;
        }
        $data = $models->toArray();
        $data['data'] = $selectData;

        return $data;
    }


    public function createOrUpdate(Array $postData, bool $is_update = false, PlayletEpisodes $playletEpisodesInfo = null){

        $playlet_number = $postData['playlet_number'];
        $playlet_number_name = $postData['playlet_number_name'];
        $playlet_id = $postData['playlet_id'];
        $amount = $postData['amount'];
        $cover = $postData['cover'];
        $play_url = $postData['play_url'];
        $playletInfo = Playlet::query()->select(['id'])->where(['id' => $playlet_id])->first();

        if(empty($playletInfo)){
            return $this->response(SystemCode::SYSTEM_ERROR,'短剧不存在');
        }
        $model = !empty($playletEpisodesInfo)? $playletEpisodesInfo : new PlayletEpisodes();
        $is_check_number = true;
        $is_check_name = true;
        if($is_update) {
            //修改时判断集数是否一样
            if ($playlet_number == $model->playlet_number) {
                $is_check_number = false;
            }
            if ($playlet_number_name == $model->playlet_number_name) {
                $is_check_name = false;
            }
        }
        if($is_check_number){
            $playletEpisodestInfo = PlayletEpisodes::query()->where(['playlet_number' =>$playlet_number,'playlet_id' =>$playlet_id])->first();
            if(!empty($playletEpisodestInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧集数已存在');
            }
            $model->playlet_number = $playlet_number;
        }
        if($is_check_name){
            $playletEpisodestInfo = PlayletEpisodes::query()->where(['playlet_number_name' =>$playlet_number_name,'playlet_id' =>$playlet_id])->first();
            if(!empty($playletEpisodestInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧集数名称已存在');
            }
            $model->playlet_number_name = $playlet_number_name;
        }
        if(!empty($cover)){
            $model->cover = $cover;
        }
        if(!empty($play_url)){
            $model->play_url = $play_url;
        }
        $model->amount = $amount;
        $model->playlet_id = $playlet_id;
        if($model->save()){
            return $this->response(SystemCode::SYSTEM_SUCCESS);
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR,'保存错误');
        }
    }

}